bbcode_mod = {

    BBCodeParser: {
        allowedTags: /^(?:img|url|b|s|u|i|colou?r|c|k|size|spoiler)$/,
        baseExp: /\[([a-z0-9]{1,8})(?:= *<a.*?href="(.*?)".*?class="externalLink".*?>.*?<\/a> *|=([a-z0-9\.#]{1,128}))?\](?: *<a.*?href="(.*?)".*?class="externalLink".*?>.*?<\/a> *)?|(?:\[\/([a-z0-9]{1,8})\])/gi,
        colorExp: /^(:?black|silver|gray|white|maroon|red|purple|fuchsia|green|lime|olive|yellow|navy|blue|teal|aqua)$/i,
        hexExp: /^#?(?:[0-9a-f]{3})?[0-9a-f]{3}$/i,

        _isValid: function (name) {
            return name && this.allowedTags.test(name);
        },

        _internal: function(sbstr, openTagName, paramUrl, param, inUrl, closeTagName, offset, fstr) {
            //console.info('m1=' + m1 + ', m2=' + m2 + ', m3=' + m3 + ', m4=' + m4 + ', m5=' + m5 );
            var self = bbcode_mod.BBCodeParser;

            if (self._isValid(openTagName)) {
                switch(openTagName) {
                    case 'spoiler':
                        return '<span style="background-color: black;color:black">';
                    case 'img':
                        if (inUrl)
                            return '<p class="externalImage"><a href="' + inUrl + '"><img class="internalImage" src="' + inUrl + '"/></a></p>';
                        else
                            return '<i>La url a la imagen no fue bien contruida, url=</i>';
                    case 'k':
                        return '<span style="text-decoration: blink;">';
                    case 'url':
                        if (paramUrl) {
                            this._urlParsing = true;
                            return '<a href="' + paramUrl + '">';
                        }
                        else if (inUrl) {
                            this._urlParsing = false;
                            return '<a href="' + inUrl + '">' + inUrl + '</a>';
                        }
                        return '<i>Url mal contruida</i>';
                    case 'c':
                    case 'color':
                    case 'colour':
                        var color = 'inherit';
                        
                        if (self.colorExp.test(param)) {
                            color = param;
                        }
                        else if (self.hexExp.test(param)) {
                            if (param.charAt(0) != '#')
                                color = '#' + param;
                            else
                                color = param;
                        }
                        
                        return '<span style="color: ' + color + ';">';
                    case 'size':
                        var size = 1;
                        if (param) {
                            size = Number(param);

                            if (size != Number.NaN)
                                size = Math.min(Math.max(size, 0.7), 3);
                        }
                        return '<span style=\"font-size: ' + size + 'em;">';
                    default:
                        return '<' + openTagName + '>';
                }
            }

            if (self._isValid(closeTagName)) {
                switch (closeTagName) {
                    case 'size':
                    case 'c':
                    case 'color':
                    case 'colour':
                    case 'spoiler':
                    case 'k':
                        return '</span>';
                    case 'img':
                        return '';
                    case 'url':
                        if (this._urlParsing)
                            return '</a>';
                        return '';
                    default:
                        return '</' + closeTagName + '>';
                }
            }

            return sbstr;
        },
        parse: function(msg) {
            return msg.replace(this.baseExp, this._internal);
        }
    },
    emoticons_baseUrl: 'http://estaticosak1.tuenti.com/layout/web2-Zero/images/chat/1_emoticon_',
    emoticons_search: [
        /^XD+|XD+ | XD+|XD+$/gi,
        /^\_*^\^| \^_*\^|\^_*\^ |\^_*\^$/gi,
        /^:\)+|:\)+ | :\)+|:\)+$/gi,
        /^:o+|:o+ | :o+|:o+$/gi,
        /^:p+|:p+ | :p+|:p+$/gi,
        /^:\(+|:\(+ | :\(+|:\(+$/gi,
        /:mobile/gi
    ],
    emoticons_replace: [
        'evilgrin.34332.png',
        'happy.34332.png',
        'happy.34332.png',
        'surprised.34332.png',
        'tongue.34332.png',
        'unhappy.34332.png',
        'mobile.37784.png'

    ],
    allowed_nodes: {
      P: ['text', 'item', 'replyText', 'info', 'body', 'blogFold'],
      H3: ['info','item']
    },
    allowed_ids: ['current_status_body'],
    allowed_pages:  [
        tuenti_lib.module.names.inicio,
        tuenti_lib.module.names.perfil,
        tuenti_lib.module.names.foto,
        tuenti_lib.module.names.historial,
        tuenti_lib.module.names.pm,
        tuenti_lib.module.names.evento,
        tuenti_lib.module.names.pagina,
        tuenti_lib.module.names.gente,
        tuenti_lib.module.names.miembros
    ],
    init: function() {
        this._prepareSmileis();
        tuenti_lib.onchangepage.add(this, true);
    },
    onChangePage: function(response){
        
        var url = (response ? response.request_object.url : document.location.href);

        if (this.allowed_pages.indexOf(system_lib.url.getParam('m',  url)) == -1)
            return;
        
        this.isSmileis = tuenti_lib.config.get().smiley;

        this._exploreNode(document.getElementById('canvas'));

        for (var i = 0; i < this.allowed_ids.length; i++) {
            var el = document.getElementById(this.allowed_ids[i]);
            if (el) this._processNode(el);
        }
    },
    _prepareSmileis: function() {
        for (var i = 0; i < this.emoticons_replace.length; i++) {
            this.emoticons_replace[i] = ' <img style="border: none" src="' + this.emoticons_baseUrl + this.emoticons_replace[i] + '"/>';
        }
    },
    _processNode: function(node) {

        var html = node.innerHTML;

        html = this.BBCodeParser.parse(html);

        if (this.isSmileis) {
            for (var i = 0; i < this.emoticons_search.length; i++) {
                html = html.replace(this.emoticons_search[i], this.emoticons_replace[i]);
            }
        }
        
        node.innerHTML = html;

        //Fix: Krasea bug
        if (node.lastChild && node.lastChild.className == 'viewMore') {
            node.addEventListener('click', this._onclickViewMore, false);
        }
    },
    _onclickViewMore: function(ev) {

        ev.preventDefault();

        var divShort = ev.target.parentNode.parentNode;
        var divFull = document.getElementById(divShort.id + '_uncropped');

        divShort.className = 'text hide';
        divFull.className = 'text';
    },

    _exploreNode: function(node) {

        var allowedParents = this.allowed_nodes[node.nodeName];

        if (allowedParents && allowedParents.indexOf(node.parentNode.className) != -1  && node.textContent.length > 1) {
            this._processNode(node);
            return;
        }

        if (node.hasChildNodes()) {
            var children = node.childNodes;
            for (var i = 0; i < children.length; i++)  {
                this._exploreNode(children[i]);
            }
        }
    }


};

bbcode_mod.init();